package cn.topevery.garden.wapi.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.topevery.garden.dal.logic.ParkFileLogic;
import cn.topevery.garden.dal.logic.ParkLogic;
import cn.topevery.garden.dal.logic.SectionParkRelationLogic;
import cn.topevery.garden.entity.po.Park;
import cn.topevery.garden.entity.po.ParkFile;
import cn.topevery.garden.entity.po.SectionParkRelation;
import cn.topevery.garden.entity.query.ParkQuery;
import cn.topevery.garden.entity.query.SectionParkRelationQuery;
import cn.topevery.garden.entity.vo.ParkVO;
import hw.topevery.framework.GeometryHelper;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.enums.GeometryTypeEnum;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author code-generate
 * @date 2021-08-06
 */
@Api(tags = "")
@RestController
@RequestMapping("/park")
public class ParkController extends BaseController {


    @Autowired
    ParkLogic parkLogic;

    @Autowired
    SectionParkRelationLogic sectionParkRelationLogic;

    @Autowired
    ParkFileLogic parkFileLogic;


    @GetMapping("/getParkById")
    public Park getParkById(Integer parkId) {
        return parkLogic.getEntity(parkId);
    }

    /**
     * 获取分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取页列表")
    @PostMapping("/listPage")
    public JsonResult<DataPager<Park>> listPage(@RequestBody ParkQuery query) {
        return JsonResult.ok(parkLogic.listPage(query));
    }

    @ApiOperation(value = "通过标段Id获取公园")
    @GetMapping("/getListBySectionId")
    public JsonResult getListBySectionId(@RequestParam Integer id) {
        SectionParkRelationQuery query = new SectionParkRelationQuery();
        query.setPageIndex(1);
        query.setPageSize(500);
        query.setSectionId(id.toString());
        DataPager<SectionParkRelation> page = sectionParkRelationLogic.listPage(query);

        List<Integer> ids = new ArrayList();
        for (SectionParkRelation sectionParkRelation : page.records) {
            ids.add(sectionParkRelation.getParkId());
        }
        List<Park> list = new ArrayList<>();
        if (ids.size() > 0) {
            list = parkLogic.getListBySectionId(ids);
        }
        return JsonResult.ok(list);
    }


    @ApiOperation("获取公园key-value数据")
    @GetMapping("/getShortList")
    public JsonResult getShortList() {
        return JsonResult.ok(parkLogic.getShortList());
    }

    /**
     * 描述 保存信息
     *
     * @param park the park
     * @return the json result
     * @author wenzhao.lin
     * @created 2021 -07-27 09:38:05 Save json result.
     */
    @ApiOperation("保存信息")
    @PostMapping("/save")
    public JsonResult save(@RequestBody Park park) {
        if (park.mapRangesPoints != null && park.mapRangesPoints.size() > 0) {
            park.geomWgs = GeometryHelper.getGeometry(GeometryHelper.getWellKnownText(park.mapRangesPoints, GeometryTypeEnum.PLANE));
            park.geomMkt = GeometryHelper.buildMercatorGeometry(park.mapRangesPoints, GeometryTypeEnum.PLANE);
        }

        Integer UserId = getUser().id;
        boolean isSave = parkLogic.saveOrUpdate(UserId.toString(), park);
        if (isSave) {
            // 保存附件
            parkFileLogic.saveOrUpdate(UserId.toString(), park.getId(), park.fileList);

            return JsonResult.ok(true);
        } else {
            return JsonResult.error("保存失败");
        }
    }

    /**
     * 描述 获取公园基础信息-详情
     *
     * @param id the id
     * @return the info by id
     * @author wenzhao.lin
     * @created 2021 -07-27 09:38:05
     */
    @ApiOperation("通过公园Id获取公园详情")
    @GetMapping("/getInfoById")
    public JsonResult getInfoById(@RequestParam Integer id) {
        try {
            Park park = parkLogic.getEntity(id);
            if (park.geomWgs != null) {
                park.mapRangesPoints = GeometryHelper.getGeometryPoint(park.geomWgs);
                park.geomWgs = null;
            }
            // 获取附件
            List<ParkFile> fileList = parkFileLogic.getListByPid(id);
            park.fileList = fileList;
            // Geometry 类型字段直接返回会报错
            ParkVO vo = BeanUtil.copyProperties(park, ParkVO.class, "geomMkt");
            return JsonResult.ok(vo);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error(e.getMessage());
        }
    }


    @ApiOperation("根据自增ID批量删除")
    @PostMapping("/deleteBatchById")
    public JsonResult<Boolean> deleteBatchById(@RequestBody Integer[] ids) {
        parkLogic.deleteBatchById(getUser().id.toString(), ids);
        return JsonResult.ok(true);
    }

    @ApiOperation("导出信息")
    @GetMapping("/export")
    public void export(ParkQuery query, HttpServletResponse response) {
        List<Park> list = parkLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "", "sheet1", Park.class, "", response);
    }

}
